package dataImport.action.manager;

import java.io.Serializable;
import java.util.HashMap;
import dataImport.action.CommandReader;
import dataImport.action.ConfigurationReader;
import dataImport.action.CustomActionReader;
import dataImport.action.DescriptionReader;
import dataImport.action.NpcReader;
import dataImport.action.ObjectReader;
import dataImport.action.PlaceReader;
import dataImport.action.PredefinedActionReader;
import dataImport.action.RequirementReader;
import dataImport.action.ScenarioReader;
import dataImport.action.StateReader;
import dataImport.action.abstracts.AbstractEntityReader;
import dataImport.exception.MissingReader;


public class EntityReaderStorage implements Serializable {

	private static final long serialVersionUID = -2634355851975255738L;

	private HashMap<EntityReaderStorageKey, AbstractEntityReader> readers;

	protected EntityReaderStorage() {
		this.readers = new HashMap<EntityReaderStorageKey, AbstractEntityReader>();
	}

	protected void add(final EntityReaderStorageKey entityReaderStorageKey, final EntityReaderManager entityReaderManager) throws MissingReader {
		this.readers.put(entityReaderStorageKey, createReader(entityReaderStorageKey, entityReaderManager));
	}

	protected AbstractEntityReader get(final EntityReaderStorageKey entityReaderStorageKey) throws MissingReader {
		if (this.contains(entityReaderStorageKey)) {
			return this.readers.get(entityReaderStorageKey);
		}

		throw new MissingReader(entityReaderStorageKey);
	}

	protected boolean contains(final EntityReaderStorageKey entityReaderStorageKey) {
		return this.readers.containsKey(entityReaderStorageKey);
	}

	private static final AbstractEntityReader createReader(final EntityReaderStorageKey entityReaderStorageKey, final EntityReaderManager entityReaderManager) throws MissingReader {
		switch (entityReaderStorageKey) {
			case COMMAND:
				return new CommandReader(entityReaderManager);
			case CUSTOM_ACTION:
				return new CustomActionReader(entityReaderManager);
			case DESCRIPTION:
				return new DescriptionReader(entityReaderManager);
			case NPC:
				return new NpcReader(entityReaderManager);
			case OBJECT:
				return new ObjectReader(entityReaderManager);
			case PLACE:
				return new PlaceReader(entityReaderManager);
			case PREDEFINED_ACTION:
				return new PredefinedActionReader(entityReaderManager);
			case REQUIREMENT:
				return new RequirementReader(entityReaderManager);
			case SCENARIO:
				return new ScenarioReader(entityReaderManager);
			case STATE:
				return new StateReader(entityReaderManager);
			case CONFIGURATION:
				return new ConfigurationReader(entityReaderManager);
		}

		throw new MissingReader(entityReaderStorageKey);
	}
}
